package com.mall.shopping.services;

import com.mall.shopping.IHomeService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Item;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.ItemMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.HomePageResponse;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class IHomeServiceImpl implements IHomeService {

    @Autowired
    ContentConverter converter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemMapper itemMapper;

    @Override
    public HomePageResponse homepage() {
        HomePageResponse response = new HomePageResponse();
        try {
//            Example example = new Example(Panel.class);
//            List list = new ArrayList();
//            list.add(1);
//            list.add(3);
//            example.createCriteria().andIn("type", list)
//                    .andEqualTo("position",0);
//            example.setOrderByClause("sort_order asc");
//            List<Panel> panels1 = panelMapper.selectByExample(example);
//            Example example2 = new Example(Panel.class);
//            example2.createCriteria().andNotIn("type",list)
//                    .andEqualTo("position",0);
//            example2.setOrderByClause("sort_order desc");
//            List<Panel> panels2 = panelMapper.selectByExample(example2);
//            ArrayList<Panel> panels = new ArrayList<>();
//            panels.addAll(panels1);
//            panels.addAll(panels2);
            Example example = new Example(Panel.class);
            example.createCriteria().andEqualTo("position", 0)
                    .andEqualTo("status", 1);
            List<Panel> panels = panelMapper.selectByExample(example);
            Set<Panel> panels1 = new HashSet<>(panels);
            Set<PanelDto> panelDtos = converter.panels2Dto(panels1);
            for (PanelDto panelDto : panelDtos) {
                Example example1 = new Example(PanelContent.class);
                example1.createCriteria().andEqualTo("panelId", panelDto.getId());
                //此处有条数限制
                example1.setOrderByClause("sort_order asc");
                List<PanelContent> panelContents = panelContentMapper.selectByExample(example1);
                List<PanelContentItemDto> panelContentItemDtos = converter.panelContents2ItemDto(panelContents);
                panelDto.setPanelContentItems(panelContentItemDtos);
                for (PanelContentItemDto panelContentItemDto : panelContentItemDtos) {
                    Item item = itemMapper.selectByPrimaryKey(panelContentItemDto.getProductId());
                    if (item != null) {
                        panelContentItemDto.setProductName(item.getTitle());
                        panelContentItemDto.setSalePrice(item.getPrice());
                        panelContentItemDto.setSubTitle(item.getSellPoint());
                    }
                }
            }
            response.setPanelContentItemDtos(panelDtos);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            response.setCode(ShoppingRetCode.SYSTEM_ERROR.getCode());
            response.setMsg(ShoppingRetCode.SYSTEM_ERROR.getMessage());
        }
        return response;
    }
}
